package Controller_Servlet;

import Model_Db.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet("/doc_update")
public class doc_update extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	    request.setCharacterEncoding("UTF-8");
	    response.setCharacterEncoding("UTF-8");
	    String user=request.getParameter("doc_username");
	    String Pwd=request.getParameter("doc_password");
	    String adnumber=request.getParameter("doc_adminumber");
	    try {
		    String sql_admincheck="select * from t_doctor where D_Id='"+user+"'"+" or D_Tel='"+user+"'";
		    DbUtil run1=new DbUtil();
		    ResultSet rs=run1.QuerySQL(sql_admincheck);
		    rs.last();
		    String number=rs.getString("AdminCheck");
		    PrintWriter out = response.getWriter();
		    if(number.equals("")==false) {
				    if(number.equals(adnumber)) {
					    String sql="update t_doctor set D_Pwd='"+Pwd+"'"+"where D_Id='"+user+"'"+" or D_Tel='"+user+"'";
					    DbUtil run=new DbUtil();
					    int n=run.updateSQL(sql);
					    if(n>=1) {
					    	request.setAttribute("outputMessage", "修改成功！");
					        request.getRequestDispatcher("注册窗口.jsp").forward(request,response);
					    	}
				    }
				    else {
				    	request.setAttribute("outputMessage", "权限码，不正确！");
				    	request.getRequestDispatcher("注册窗口.jsp").forward(request,response);
				    }
			    	}
		    
		    //等于空值的时候
		    else {
		    	String sql="update t_doctor set D_Pwd='"+Pwd+"'"+"where D_Id='"+user+"'"+" or D_Tel='"+user+"'";
		    	DbUtil run=new DbUtil();
		    	int n=run.updateSQL(sql);
		    	if(n>=1) {
		    		request.getRequestDispatcher("注册窗口.jsp").forward(request,response);
		    	}
	
		    }
		    }
	    catch(SQLException e) {
	    }
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}